
#import "Reader.h"

@implementation Reader


- init:( char*) portName baudrate:(int) baudRate
{
int ok;
	[super init];
	strcpy(readString,"");
	termChar = '\r';		//most common.
	target = nil;
 	ok = [self openRAW:portName Baudrate:baudRate ];
	if( ok == SERIALOK)
	{
		[self setTerm:'\r'];
	}
	else
	{
		NXRunAlertPanel("Reader","%s\nDid Not Open","OK",NULL,NULL,portName);
		[NXApp terminate:nil];
	}
	[self setNotify:@selector(charsReady) with:self];
	[self setCharacterTimeout: 5 * SEC];
	return self;
}


- (SEL) action
{
	return action;
}

- setAction:(SEL) aSelector
{
	action = aSelector;
	return self;
}

- setTarget:anObject
{
	target = anObject;
	return self;
}

- target
{
	return target;
}

- setTarget:(SEL) theTarget andID: theID
{
	action  = theTarget;
	target = theID;
	return self;
}

- setTerm:(char) term
{
	termChar = term;
	return self;
}

//Now for the internal methods...
- charsReady
{
int numRead;
BOOL ok = YES;

	numRead = [self readString: readString forMaxOf:READSTRINGSIZE -1 term:termChar];
	//do any parsing or error checking here
	//set ok = NO...
	if( ok )
	{
		if( target != nil)
			[ target perform:action with:self];
	}
	else
	{
		NXBeep();
	}
	return self;
}

- (const char*) stringValue
{
	return readString;
}

@end


